#!/usr/bin/python3
# -*- coding: utf-8 -*-

#读取备份文件列表
def read_backupsname(filePath):
    import os
    name = os.listdir(filePath)#获取filepath的文件列表
    print(name)
    return name
    
# print(len(read_backupsname('./backups/')))

#创建不带logo二维码
def qrcode_make(information):
    import qrcode
    # information = input("输入二维码包含的信息:\n")
    #调用qrcode的make()方法传入url或者想要展示的内容
    img = qrcode.make(str(information))
    # print(img.save)
    #写入显示文件
    with open('show.png', 'wb') as f1:
        img.save(f1)
        
    print(len(read_backupsname('./backups/')))
    #写入备份文件
    with open('backups/backups'+str(len(read_backupsname('./backups/')))+'.png', 'wb') as f2:
        img.save(f2)
        
# qrcode_make('www.baidu.com')

#创建带logo二维码
def qrcode_make_logo(information,logo,versions):
    import qrcode   
    from PIL import  Image
    
    qr = qrcode.QRCode(     
        version=versions, #1~5    
        error_correction=qrcode.constants.ERROR_CORRECT_L,     
        box_size=10,     
        border=4, 
    ) #创建QRCode对象
    if information=='':
        print('请输入有效信息')
        return False
    else:
        qr.add_data(data=str(information)) #添加数据
        qr.make(fit=True)# 更改二维码的颜色，默认是黑色 
        
        img = qr.make_image(fill_color="#000000", back_color="white")#make_image(fill_color="#ff3300", back_color="white")
        # print(img.size)#(290,290)
        #获取二维码的宽和高
        img_size=img.size
        img_w,img_h=img_size
        print(img_w,img_h)#410 410
        
        icon = Image.open(logo)#打开图标
        # print(icon)
        # 设置小图标的大小，缩放的时候尺寸必须是整数，否则报错
        icon_w,icon_h = int(img_w/6) ,int(img_h/6)
        print(icon_w,icon_h)#68 68
        
        icon_small = icon.resize((icon_w,icon_h),Image.ANTIALIAS)  # Image.ANTIALIAS 画面平滑缩放
        # 获取粘贴小图标的坐标，必须整数，否则粘贴的时候，报错
        icon_x, icon_y = int((img_w-icon_w)/2 ), int((img_h-icon_h)/2)
        print(icon_x,icon_y)#171 171
        # 把图标“粘贴”到二维码上正中间。
        img.paste(icon_small,(icon_x,icon_y))
        
        # img.show()
        
        
        #写入显示文件
        with open('show.png', 'wb') as f1:
            img.save(f1)
            
        print(len(read_backupsname('./backups/')))
        #写入备份文件
        with open('backups/backups'+str(len(read_backupsname('./backups/')))+'.png', 'wb') as f2:
            img.save(f2)
         
        return True

# qrcode_make_logo(286153101803429915,'logo.jpg',4)

# 识别二维码 图
def qrcode_recognition_pic(pic):
    import cv2
    import numpy as np
    # print(pic)
    if pic!='':
        # 读取二维码
        # src = cv2.imread(pic)#中文路径产生bug
        src = cv2.imdecode(np.fromfile(pic,dtype = np.uint8),-1)#解决中文路径问题
        # gray = cv2.cvtColor(src, cv2.COLOR_BGR2GRAY)
        # 设置检测器
        qrcoder = cv2.QRCodeDetector()
        # 检测识别二维码
        codeinfo, points, straight_qrcode = qrcoder.detectAndDecode(src)
        # print(points,straight_qrcode)
        print(codeinfo, points, straight_qrcode)
        result = np.copy(src)
        # cv2.drawContours(result, [np.int32(points)], 0, (0, 0, 255), 2)
        # 输出识别二维码的信息
        print("qrcode information is : \n%s"% codeinfo)
        # 显示图片
        # cv2.imshow("result", result)
        # cv2.imshow("qrcode roi", np.uint8(straight_qrcode))
        cv2.waitKey(0)
        cv2.destroyAllWindows()
        
        return codeinfo
    else:
        return -1
# a='E:/AI/二维码识别/logo.jpg'
# ret = qrcode_recognition_pic(str(a))
# if ret!='':
    # print(ret,'good')


# 识别二维码 摄像头或视频 输入视频名或摄像头符号0或1
def qrcode_recognition_video(inputs):
    import cv2
    import time

    cap = cv2.VideoCapture(inputs)
    print(cap,cap.isOpened())
    if cap.isOpened() == False:
        codeinfo=False
    
    t_start = time.time()
    # print(t_start) 
    while cap.isOpened() == True:
        ret,frame = cap.read()
        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        
        # 设置检测器
        qrcoder = cv2.QRCodeDetector()
        # print(qrcoder.rect)
        # 检测识别二维码
        # print(qrcoder.detectAndDecode(gray))
        codeinfo, points, straight_qrcode = qrcoder.detectAndDecode(gray)
        # print(codeinfo, points, straight_qrcode)
        # cv2.drawContours(frame, contours, 0, (0, 0, 255), 2)
        # 输出识别二维码的信息
        # print(len(codeinfo))
        t_stop = time.time()
        t = t_stop-t_start
        # print(t)
        if len(codeinfo) == 0:
            print('not qrcode')
        else:
            t_start = time.time()
            print("qrcode information is : \n%s"%codeinfo)
            break
            
        if int(t) > 15 and len(codeinfo) == 0:
            break
            
        cv2.imshow("cap", frame)
        if cv2.waitKey(10)&0xff == ord('q'):
            break
    cap.release()
    cv2.destroyAllWindows()
    
    return codeinfo

# information = qrcode_recognition_video(1)
# print(information)